package utils

import android.annotation.SuppressLint
import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions

class ImageLoader {
    /**
     * 加载普通图片
     */
     fun loadImage(context:Context,url: String,imageView:ImageView){
         Glide.with(context).load(url).into(imageView)
    }
    /**
     * 加载普通图片
     */
    fun loadCircle(context: Context,url: String,imageView: ImageView){
        Glide.with(context).load(url).circleCrop().into(imageView)
    }
    /**
     * 加载圆角图片
     */
    @SuppressLint("CheckResult")
    fun loadRound(context: Context, url: String, imageView: ImageView, radius: Int){
        val options = RequestOptions().transform(RoundedCorners(radius))
        Glide.with(context).load(url)
            .apply(options)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(imageView)
    }

    /**
     * 加载带占位图和错误图的图片
     */
    fun loadWithPlaceholder(context: Context?, url: String?,
        imageView: ImageView?,
        placeholderRes: Int,
        errorRes: Int
    ) {
        Glide.with(context!!)
            .load(url)
            .placeholder(placeholderRes)
            .error(errorRes)
            .into(imageView!!)
    }
}